//

		Include("data/scripts/enemy.nut")

class	Turret	extends Enemy
{

	target_name			=	0
	target_item			=	0
	project_script		=	0
	turret_body			=	0
	look_at				=	0
	turret_cannon		=	0
	scene				=	0
	shoot_range			=	Mtr(10.0)
	cannon 				=	0
	shoot_timeout		=	0.0

	function	OnUpdate(item)
	{
		base.OnUpdate(item)

		local	_target_pos = ItemGetWorldPosition(target_item)

			look_at = _target_pos.Lerp(0.05, look_at)
			look_at.y = ItemGetWorldPosition(item).y
			ItemSetTarget(turret_body, look_at)

		if (ItemGetWorldPosition(item).Dist(_target_pos) < shoot_range)
		{
			if ((g_clock - shoot_timeout) > SecToTick(Sec(1.0)))
			{
				shoot_timeout = g_clock
				cannon.Fire()
			}
		}

	}

	function	FetchRangeFromHelper(item)
	{
		local	_parent
		_parent = ItemGetParent(item)
		if (ObjectIsValid(_parent))
		{
			print("Turret::FetchRangeFromHelper() found parent item : '" + ItemGetName(_parent) + "'")

			local	_helper
			_helper = ItemGetChild(_parent, "range")
			if (ObjectIsValid(_helper))
			{
				shoot_range = ItemGetScale(_helper).x / 2.0
				ItemActivate(_helper, false)
				print("Turret::FetchRangeFromHelper() found a helper, shoot_range = " + shoot_range.tostring() + " meters.")
			}
		}
	}

	function	OnSetup(item)
	{
		print("Turret::OnSetup()")

		base.OnSetup(item)
		target_name = "tank"

		project_script = ProjectGetScriptInstance(g_project)

		scene = ItemGetScene(item)
		turret_body = ItemGetChild(item, "body")
		look_at = Vector(0,0,0)

		cannon = Cannon(ItemGetChild(turret_body, "bullet_out"))
		shoot_timeout = g_clock

		FetchRangeFromHelper(item)
	}

	function	OnSetupDone(item)
	{
		target_item = SceneFindItem(scene, "tank")
	}
}
